﻿using System.Web;
using System.Web.Script.Serialization;
using ElGee.Google.Charts;
using System.Text;

namespace WebApp.LineChartSample
{
    /// <summary>
    /// Summary description for DataSource
    /// </summary>
    public class DataSource : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // No Cache, because need fresh values !
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            // Set the content type and encoding for JSON
            context.Response.ContentType = "application/json";
            context.Response.ContentEncoding = Encoding.UTF8;

            // Find the Construction of the DataSource in /Data/LineChartData
            LineChart lc = LineChartData.GetOptions();

            // Changing the Title to show you the options can be set or changed in the Asynchronous call
            lc.Style.Title += " with Async Call";

            ChartDataSource cds = LineChartData.GetDataSource();
            // Chaging the Values of the Chart, just for fun
            cds.Tables[1].Data[10] = "5";

            // Create the object needed for All Asynchronous calls. Must be a LineChartDataSource
            LineChartDataSource ds = new LineChartDataSource(cds, lc);

            // Returning the object in JSON Format
            context.Response.Write(new JavaScriptSerializer().Serialize(ds));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}